<?php

	function listFile($dir)
	{
	    if(is_dir($dir))
		{  
			if($dirHandle = opendir($dir))
			{
				$i = 0;
				while ($fileName = readdir($dirHandle)) 
				{
					if($fileName=='.' || $fileName == '..' || $fileName == 'index.php')
					{
						continue;
					}
					else
					{
						$arr[$i++] = $fileName;
					}
				}
			}
		}
		return $arr;
	}

	function getHeader()
	{
		include(ADMINPATH.'header.php');
	}
	
	function getPanel()
	{
		include(ADMINPATH.'panel.php');
	}
	
	function getFooter()
	{
		include(ADMINPATH.'footer.php');
	}
	
	function setAdminSession($userName,$uid)
	{
		$_SESSION['adminName'] = $userName;
		$_SESSION['adminUid'] = $uid;
		$_SESSION['adminPassed'] = 'TRUE';
	}
	
	function adminLogin($userName,$userPwd)
	{
		$conn = new mysqlClass();
		$query = "SELECT userPwd,uid FROM users WHERE userLevel='admin' AND userName='{$userName}';";
		$conn->query($query);
		$conn->fetch();
		if ($conn->data['userPwd'] == md5($userPwd))
		{
			setAdminSession($userName,$conn->data['uid']);
			return 0;
		}
		else
		{
			return 1;
		}
	}
	
	function addUser($userName)
	{
		$conn = new mysqlClass();
		$query = "SELECT userName FROM users WHERE userName='{$userName}';";
		$conn->query($query);
		if ($conn->row >= 1)
		{
			return 'NO';
		}
		else
		{
			$query = "SELECT value FROM record WHERE name='userAmount';";
			$conn->query($query);
			$conn->fetch();
			$uid = $conn->data['value'] + 1;
			$pwd = $userName.substr(time(), 6);
			$md5Pwd = md5($pwd);
			$query = "INSERT INTO users (uid,userName,userPwd) VALUES ({$uid},'{$userName}','{$md5Pwd}');";
			if ($conn->query($query))
			{
				$query = "UPDATE record SET value='{$uid}' WHERE name='userAmount';";
				if ($conn->query($query))
				{
					return $pwd;
				}
				else
				{
					return 'NO';
				}
			}
			else
			{
				return 'NO';
			}
		}
	}
	
	function getEditor()
	{
		echo "<link rel='stylesheet'	href='".PLUGINURL."tinyEditor/style.css' />
			  <script type='text/javascript'	src='".PLUGINURL."tinyEditor/tinyeditor.js'></script>
				<script type='text/javascript'>
				var instance =new TINY.editor.edit('editor',{
				id:'tinyInput',
				width:584,
				height:175,
				cssclass:'te',
				controlclass:'tecontrol',
				rowclass:'teheader',
				dividerclass:'tedivider',
				controls:['bold','italic','underline','strikethrough','|','subscript','superscript','|',
			  		'orderedlist','unorderedlist','|','outdent','indent','|','leftalign',
			  		'centeralign','rightalign','blockjustify','|','unformat','|','undo','redo','n',
			  		'font','size','style','|','image','hr','link','unlink','|','cut','copy','paste','print'],
				footer:true,
				fonts:['Verdana','Arial','Georgia','Trebuchet MS'],
				xhtml:true,
				cssfile:'style.css',
				bodyid:'editor',
				footerclass:'tefooter',
				toggle:{text:'source',activetext:'wysiwyg',cssclass:'toggle'},
				resize:{cssclass:'resize'}
				});
			</script> </textarea>";
	}
?>